import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:freemusic/components/GedanDialog.dart';
import 'package:freemusic/components/WidgetTitle.dart';
import 'package:freemusic/page/song/LocalSongPage.dart';
import 'package:freemusic/page/song/RecentSongPage.dart';
import 'package:freemusic/page/songsheet/CloudSongSheet.dart';
import 'package:freemusic/utils/Util.dart';
import 'package:permission_handler/permission_handler.dart';

class FunctionWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return FunctionState();
  }
}

class FunctionState extends State<FunctionWidget> {

  @override
  void initState() {
    super.initState();
  }

    Future<bool> _checkPermission() async {
    PermissionHandler permissionHandler = PermissionHandler();
    PermissionGroup notificationPermission = PermissionGroup.notification;
    PermissionStatus status =
        await permissionHandler.checkPermissionStatus(notificationPermission);
    if (status != PermissionStatus.granted) {
      Map<PermissionGroup, PermissionStatus> permissions =
          await permissionHandler
              .requestPermissions([PermissionGroup.notification]);
      status = permissions[notificationPermission];
      if (status != PermissionStatus.granted) {
        return false;
      }
    }
    return true;
  }

  void _sendNotification() {
    MethodChannel channel = MethodChannel("com.jt.freemusic/flutter");
    channel.invokeMethod("notification", {
      "img": "http://img1.imgtn.bdimg.com/it/u=4238142487,3274484296&fm=26&gp=0.jpg",
      "songName": "七里香",
      "singerName": "周杰伦",
    });
  }

  void handleAddGedanOnTap() {
    showDialog(
        context: context,
        builder: (ctx) {
          return GedanDialog();
        });
    // GedanServices.getInstace().loadGedanList();
  }

  Widget _renderSheetTitle() {
    Icon icon = Icon(
      Icons.playlist_add,
      color: Util.wordColor,
      size: 28,
    );
    return WidgetTitle(
      "自建歌单",
      onTap: this.handleAddGedanOnTap,
      icon: icon,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(left: 14, right: 14, bottom: 30),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              FunctionButton(
                icon: "images/shouji.png",
                title: "本地音乐",
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => LocalSongPage()));
                  // _sendNotification();
                },
              ),
              // FunctionButton(
              //   icon: "images/favorite.png",
              //   title: "我喜欢",
              // ),
              FunctionButton(
                icon: "images/yun.png",
                title: "同步云歌单",
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => CloudSongSheetPage()));
                },
              ),
              FunctionButton(
                icon: "images/zuijin.png",
                title: "最近播放",
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => RecentSongPage()));
                },
              ),
            ],
          ),
        ),
        this._renderSheetTitle(),
      ],
    );
  }
}

class FunctionButton extends StatelessWidget {
  final double _iconsize = 48;

  final String icon;
  final String title;
  final GestureTapCallback onTap;

  FunctionButton({this.icon, this.title, this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: this.onTap,
      child: Column(
        children: <Widget>[
          Container(
            child: Image.asset(
              this.icon,
              width: this._iconsize,
              height: this._iconsize,
            ),
            margin: EdgeInsets.only(bottom: 10),
          ),
          Text(
            this.title,
            style: TextStyle(color: Util.wordColor),
          ),
        ],
      ),
    );
  }
}
